package com.java.service;

import com.java.dao.EnterGoodMapper;
import com.java.dao.EnterGoodsMapper;
import com.java.pojo.EnterGood;
import com.java.pojo.EnterGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;


@Service
public class EnterGoodsServiceImpl implements EnterGoodsService {

    @Autowired
    private EnterGoodsMapper enterGoodsMapper;
    @Autowired
    private EnterGoodMapper enterGoodMapper;


    @Override
    public List<EnterGoods> getListByGoodS(Integer eid, String document) {
        return enterGoodsMapper.getListByGoodS(eid,document);
    }

    /*根据外键查总个数*/
    @Override
    public Integer getCountByEid(Integer eid) {
        return enterGoodsMapper.getCountByEid(eid);
    }

    //删除
    @Override
    public int deleteEnterGood(String eid) {
        return enterGoodsMapper.deleteEnterGoodByEid(eid);
    }

    @Override
    public int deleteEnterGoodByEgid2(String egid) {
        return enterGoodsMapper.deleteEnterGoodByEgid(Integer.parseInt(egid));
    }

    @Override
    public boolean deleteEnterGoodByEgid(String egid) {
        System.out.println("---"+egid);
        //获取entergood和entersgood
        EnterGoods goods = enterGoodsMapper.getEnterGoodsByEid(Integer.parseInt(egid));
        Integer egid1 = goods.getEgid();//获取外键
        EnterGood good = enterGoodMapper.getEnterGoodByEGid(egid1);//通过外键获取进货订单
        System.out.println("good:"+good+",goods:"+goods);

        //获取goods和good的num,money,goodname
        Double goodsMoney = goods.getMoney();
        Integer goodsNum = goods.getNum();
        String goodsGoodname = goods.getGoodname2();
        Double goodMoney = good.getMoney();
        Integer goodNum = good.getNum();
        String goodGoodname = good.getGoodname();

        //创建EnterGood的对象
        EnterGood enterGood=new EnterGood();

        //删除后的EnterGood数据
        Integer nwNum=0;
        String nwGoodName="";
        Double nwMoney=0.0;
        nwNum = goodNum - goodsNum;
        nwGoodName = goodGoodname.replace(goodsGoodname, "");
        nwMoney=goodMoney-goodsMoney;

        //赋值
        enterGood.setNum(nwNum);
        enterGood.setMoney(nwMoney);
        enterGood.setGoodname(nwGoodName);
        enterGood.setEid(egid1);
        System.out.println(enterGood.toString());

        //操作  删除  修改
        int i = enterGoodsMapper.deleteEnterGoodByEid(egid);
        int i1 = enterGoodMapper.updateEnterGoodByEnterGoodsByDel(enterGood);
        System.out.println("删除"+i+",修改:"+i1);
        if(i>0&&i1>0){
            return true;
        }else {
           return false;
        }
    }

    //修改entergoods
    @Override
    public int updateByEnterGoodAndEnterGoods(Integer eid,Integer num,Double money) {
        System.out.println("[---]"+eid);
        //获取entergood和entersgood
        EnterGoods goods = enterGoodsMapper.getEnterGoodsListByEgid2(eid);
        System.out.println("=====goods:"+goods);
        EnterGood good = enterGoodMapper.getEnterGoodByEGid(goods.getEgid());//通过外键获取进货订单
        System.out.println("good:"+good+",goods:"+goods);


        //创建俩变量,一会修改entergood
        Integer GoodNum=0;
        Double GoodMoney=0.0;

        if(GoodNum==null ||"".equals(GoodNum)){
            GoodNum=0;
        }
        if(GoodMoney==null ||"".equals(GoodMoney)){
            GoodMoney=0.0;
        }

        GoodNum=good.getNum()-goods.getNum()+num;
        GoodMoney=good.getMoney()-goods.getMoney()+money;
        //创建EnterGood
        EnterGood enterGood=new EnterGood();
        enterGood.setEid(goods.getEgid());
        enterGood.setNum(GoodNum);
        enterGood.setMoney(GoodMoney);
        System.out.println("entergood:"+enterGood);

        //创建EnterGoods
        EnterGoods enterGoods=new EnterGoods();
        enterGoods.setEgid(eid);
        enterGoods.setNum(num);
        enterGoods.setMoney(money);



        //操作:修改entergood和entergoods
        int i1 = enterGoodMapper.updateEnterGoodByEnterGoodsByDel2(enterGood);
        int i = enterGoodsMapper.updateGoods(enterGoods);


        return i+i1;
    }


    @Override
    public List<EnterGoods> getGoodsByGoodStyle(Integer a) {
        return enterGoodsMapper.getGoodsByGoodStyle(a);
    }

    @Override
    public int updOknandNo3(String g) {
        return enterGoodsMapper.updOknandNo3(g);
    }


    /*查询根据id*/
    @Override
    public EnterGoods getEnterGoodsByEid(Integer eid) {
        return enterGoodsMapper.getEnterGoodsByEid(eid);
    }


    /*添加*/
    @Override
    public boolean addEnterGoods(EnterGoods enterGoods) {
        System.out.println("enterGoods:"+enterGoods);

        //查询enterGood
        Integer egid = enterGoods.getEgid();//获取外键
        EnterGood good = enterGoodMapper.getEnterGoodByEGid(egid);//根据外键查询

        //创建几个变量
        Integer nwNum=0;
        String nwGoodName="";
        Double nwMoney=0.0;

        //找出根据外键查询出来的EnterGood
        Integer goodNum = good.getNum();
        Double goodMoney = good.getMoney();
        String goodGoodname = good.getGoodname();
        if("".equals(goodGoodname)||null==goodGoodname){
            goodGoodname=" ";
        }
        if(goodNum==null ||"".equals(goodNum)){
            goodNum=0;
        }
        if(goodMoney==null ||"".equals(goodMoney)){
            goodMoney=0.0;
        }
        String document2="";


        /*编号*/
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyyMMdd");
        String newDate=sdf2.format(new Date());
        String result="";
        String s="CP";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        System.out.println(s+newDate+result);

        //给变量赋值
        StringBuffer sb=new StringBuffer(goodGoodname);
        if("".equals(sb)){
            StringBuffer append = sb.append(enterGoods.getGoodname2());
        }
        StringBuffer append = sb.append("," + enterGoods.getGoodname2());
        nwGoodName=append.toString();
        nwNum=goodNum+enterGoods.getNum();
        nwMoney=goodMoney+enterGoods.getMoney()*enterGoods.getNum();
        document2=s+newDate+result;

        //赋值
        enterGoods.setDocument2(document2);


        //重新给根据外键获取出来的enterGood赋值
        good.setGoodname(nwGoodName);
        good.setNum(nwNum);
        good.setMoney(nwMoney);

        //操作  添加 修改
        int i=0;
        EnterGoods goods = enterGoodsMapper.getEnterGoodsBydocument2(document2);
        EnterGoods es = enterGoodsMapper.getES(enterGoods.getGoodname2());
        //查询如果没有直接天界
        if(es==null){
             i = enterGoodsMapper.addEnterGoodS(enterGoods);
            System.out.println("000"+i);
        }else{
            //如果有修改其num即可
            int i1 = enterGoodsMapper.updES(enterGoods.getGoodname2(), enterGoods.getNum());
            System.out.println("==="+i1);
        }

        int i1 = enterGoodMapper.updateEnterGoodByEnterGoodsByDel(good);
        System.out.println("o"+i1);
        if(i>0&&i1>0){
            return true;
        }else {
        return false;
        }
    }



    @Override
    public List<EnterGoods> getEnterGoodsListByEgid(Integer egid) {
      return enterGoodsMapper.getEnterGoodsListByEgid(egid);
    }

    @Override
    public int updateByOkAndNo2(Integer a) {
        return enterGoodsMapper.updateByOkAndNo2(a);
    }

    @Override
    public EnterGoods findEnByName(String goodname2) {
        return enterGoodsMapper.findEnByName(goodname2);
    }

    @Override
    public int updOknandNo3_2(String goods2) {
        return enterGoodsMapper.updOknandNo3_2(goods2);
    }


}
